﻿using System.IO;
using System.Web.Mvc;

namespace smp.ButterflyCakes.Web.Infrastructure
{
    public static class ExtensionMethods
    {

        //return new JsonResult
        //    {
        //        Data = new
        //        {
        //            success = true,
        //            data = data,
        //            view = this.RenderPartialView("Edit", data)
        //        },
        //        JsonRequestBehavior = JsonRequestBehavior.AllowGet
        //    };
        public static string RenderPartialView(this Controller controller, string viewName, object model)
        {
            if (string.IsNullOrEmpty(viewName))
                viewName = controller.ControllerContext.RouteData.GetRequiredString("action");

            controller.ViewData.Model = model;
            using (var sw = new StringWriter())
            {
                ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
                var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
                viewResult.View.Render(viewContext, sw);

                return sw.GetStringBuilder().ToString();
            }
        }
    }
}